package com.weimei.conversionTreasure.viewModel;

import android.app.Activity;
import android.app.Application;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;

import com.lxj.xpopup.XPopup;
import com.weimei.conversionTreasure.http.Repository;
import com.weimei.conversionTreasure.http.entity.ComputerBean;
import com.weimei.conversionTreasure.http.entity.SheRuBean;
import com.weimei.conversionTreasure.utils.ValidateUtils;
import com.weimei.conversionTreasure.weight.BottomTimePop;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.observers.DisposableObserver;
import me.goldze.mvvmhabit.base.BaseModel;
import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.binding.command.BindingAction;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.utils.RxUtils;
import me.goldze.mvvmhabit.utils.ToastUtils;

public class SheRuVM extends BaseViewModel<Repository> {
    public ObservableField<String> Drinking = new ObservableField<>("");
    public ObservableField<String>  drinkingVolume = new ObservableField<>("");
    public ObservableField<String>  alcoholDegree = new ObservableField<>("");

    public ObservableField<String>  result = new ObservableField<>("");
    public ObservableField<String>  result1 = new ObservableField<>("");

    Activity mActivity;
    @Override
    public void setActivity(Activity activity) {
        super.setActivity(activity);
        this.mActivity =activity;
    }

    public BindingCommand timePick = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            new XPopup.Builder(mActivity)
                    .moveUpToKeyboard(false)//如果不加这个，评论弹窗会移动到软键盘上面
                    .enableDrag(true)
                    .asCustom(new BottomTimePop(mActivity, s1 -> {
                        Drinking.set(s1);
                    },true,true))
                    .show();
        }
    });

    public BindingCommand js = new BindingCommand(() -> {
        if (ValidateUtils.isEmptyString(Drinking.get())){
            ToastUtils.showShort("请输入饮酒时间");
            return;
        }

        if (ValidateUtils.isEmptyString(drinkingVolume.get())){
            ToastUtils.showShort("请输入饮酒量");
            return;
        }

        if (ValidateUtils.isEmptyString(alcoholDegree.get())){
            ToastUtils.showShort("请输入饮酒度数");
            return;
        }

        Map<String,String> map = new HashMap<>();
        map.put("Drinking",Drinking.get());
        map.put("drinkingVolume",drinkingVolume.get());
        map.put("alcoholDegree",alcoholDegree.get());
        model.intake_calculation(map)
                .compose(RxUtils.schedulersTransformer())
                .compose(RxUtils.exceptionTransformer())
                .doOnSubscribe(this)
                .doOnSubscribe(disposable -> {})
                .subscribe(new DisposableObserver<SheRuBean>() {
                    @Override
                    public void onNext(SheRuBean baseResult) {
                        if (baseResult.issucc){
                            result.set(baseResult.getData().getIntake()+"");
                            result1.set(baseResult.getData().getLevel()+"");
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    });

    public SheRuVM(@NonNull Application application, Repository model) {
        super(application, model);
    }
}
